﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace iuPlace.Web.Extensions
{
    public static class ViewPage
    {
        public static string ApplicationUrl(this UrlHelper url)
        {
            Uri contextUri = new Uri(url.RequestContext.HttpContext.Request.Url, url.RequestContext.HttpContext.Request.RawUrl);
            UriBuilder realmUri = new UriBuilder(contextUri) { Path = url.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
            //return realmUri.Path == "/" ? realmUri.ToString().Remove(realmUri.ToString().Length - 1) : realmUri.ToString().Replace(realmUri.Path, "");
            return realmUri.ToString() + (realmUri.Path != "/" ? "/" : "");
        }

        public static MvcHtmlString JsonToString(this HtmlHelper html, object data)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return MvcHtmlString.Create(serializer.Serialize(data));
        }
    }
}